﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PSLib.CodeFormating;
using PSLib.CodeFormating.Formatters;
using PSLib.CodeFormating.Lexems;
using PSLib.CodeFormating.Adapters;

namespace LuaCodeStyler
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
			//textBoxInput.Text = Properties.Resources.testLua;
			Templates.FormatListChanged += new EventHandler(Templates_FormatListChanged);
			Templates.FormatActiveIndexChanged += new EventHandler(Templates_FormatActiveIndexChanged);
            
			UpdateFormatList();
		}

		private void Templates_FormatActiveIndexChanged(object sender, EventArgs e)
		{
			UpdateFormatList();
		}

		private void Templates_FormatListChanged(object sender, EventArgs e)
		{
			UpdateFormatList();
		}

		private void exitToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void loadSourceToolStripMenuItem_Click(object sender, EventArgs e)
		{
			OpenFileDialog d = new OpenFileDialog();
			if (d.ShowDialog() == DialogResult.OK)
			{
				try
				{
					textBoxInput.Text = System.IO.File.ReadAllText(d.FileName);
				}
				catch (Exception ex)
				{
					MessageBox.Show("Cannot load source text:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
			}
		}

		private void saveSourceToolStripMenuItem_Click(object sender, EventArgs e)
		{
			SaveFileDialog d = new SaveFileDialog();
			if (d.ShowDialog() == DialogResult.OK)
			{
				try
				{
					System.IO.File.WriteAllText(d.FileName, textBoxInput.Text);
				}
				catch (Exception ex)
				{
					MessageBox.Show("Cannot save source text:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				
			}
		}

		private void saveResultToolStripMenuItem_Click(object sender, EventArgs e)
		{
			SaveFileDialog d = new SaveFileDialog();
			if (d.ShowDialog() == DialogResult.OK)
			{
				try
				{
					System.IO.File.WriteAllText(d.FileName, textBoxOutput.Text);
				}
				catch (Exception ex)
				{
					MessageBox.Show("Cannot save result text:\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}

			}
		}

		private void copySourceToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Clipboard.Clear();
			Clipboard.SetData(DataFormats.Text, textBoxInput.Text);
		}

		private void copyResultToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Clipboard.Clear();
			Clipboard.SetData(DataFormats.Text, textBoxOutput.Text);
		}

		private void manageTemplateToolStripMenuItem_Click(object sender, EventArgs e)
		{
			TemplateManagerForm manager = new TemplateManagerForm();
			manager.ShowDialog();
		}

		private void processInputToolStripMenuItem_Click(object sender, EventArgs e)
		{
			if(Templates.ActiveFormatIndex != -1)
			{
				FormatConfiguration sett = Templates.GetFormat(Templates.ActiveFormatIndex);
				//Formatter.LuaFormatter
				textBoxOutput.Text = Formatter.Format(textBoxInput.Text, LanguageAdapter.LuaAdaptor, sett);
			}
		}

		private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
		{
			aboutForm about = new aboutForm();
			about.ShowDialog();
		}

		private void UpdateFormatList()
		{
			toolStripComboBoxTemplates.Items.Clear();
			foreach (var item in Templates.SettingsList)
				toolStripComboBoxTemplates.Items.Add(item);

			toolStripComboBoxTemplates.SelectedIndex = Templates.ActiveFormatIndex;
		}

		private void toolStripComboBoxTemplates_SelectedIndexChanged(object sender, EventArgs e)
		{
			Templates.ActiveFormatIndex = toolStripComboBoxTemplates.SelectedIndex;
		}

		private void manualToolStripMenuItem_Click(object sender, EventArgs e)
		{
			string helpPath = Application.StartupPath;
			System.Diagnostics.Process.Start("explorer", @"http://sites.google.com/site/pslibdevelop/lua/lua-code-styler");
		}
	}
}
